﻿using helloworld2.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace helloworld2
{
    class Road
    {

        public int id;
        public double length;
        List<KeyValuePair<int, int>> field = new List<KeyValuePair<int, int>>();
        RoadContainer rc = RoadContainer.Instance;
        VertexContainer vc = VertexContainer.Instance;
        City start;
        City end; //The cities the road is connected to
        List<Object> connections = new List<Object>(); 
        

        public Road(int id, List<KeyValuePair<int, int>> field, City start, City end)
        {

            this.id = id;
            this.field = field;
            this.start = start;
            this.end = end;
            this.length = findLength();

            if (this.field.Count > 3)
            {
                Edge e = new Edge(this.id, this.length);
            }
           

            List<KeyValuePair<int, int>> vertexField = new List<KeyValuePair<int, int>>();
            for (int i = 0; i < 3; i++)
            {
                vertexField.Add(this.field[i]);
            }

            Vertex v = new Vertex(vertexField, vc.id);
            vc.id++;
        }

        public Road()
        {
        }

        public List<KeyValuePair<int, int>> getField()
        {
            var newField = new List<KeyValuePair<int, int>>();
            foreach (KeyValuePair<int, int> kp in field)
            {
                newField.Add(kp);
            }
            return newField;
        }

        private double findLength()
        {
            KeyValuePair<int, int> start = this.field[0];
            KeyValuePair<int, int> end = this.field[field.Count-1];

            double a;
            double b;

            if (this.field.Count == 3)
            {
                return 1.2;
            }

            else
            {
                if (start.Value > end.Value)
                {
                    a = start.Value - end.Value;
                }

                else
                {
                    a = end.Value - start.Value;
                }

                if (start.Key > end.Key)
                {
                    b = start.Key - end.Key;
                }

                else
                {
                    b = end.Key - start.Key;
                }

                double result = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
                result = result * 1.2;
                return result;
            }

           
        }

        public KeyValuePair<int, int> getIndexOfField(int i)
        {
            return this.field[i];
        }

        public void addConnection(Object o)
        {
            connections.Add(o);
        }

        public List<Object> getConnection()
        {
            List<Object> recon = new List<Object>();
            foreach (Object o in connections)
            {
                recon.Add(o);
            }
            return recon;
        }

        public bool lookForCoordinates(KeyValuePair<int, int> coordinates)
        {
            bool re = false;
            if (this.field.Contains(coordinates))
            {
                re = true;
            }
            return re;
        }

    }
}
